Welcome back. In this lesson,
we're going to learn about some operations that we can use to modify dictionaries.
So here on line 1,
we create a dictionary.
In this dictionary, we have four key-value pairs.
So the key pears is associated with the integer 217.
The key and the string apples is associated with 430, and so on.
So, overall we have four key-value pairs and that's all created on line 1.
Now, on line 2, we do an operation called del.
So, del is short for Delete,
and Delete deletes a key-value pair from our dictionary.
So, when we say "del" and then we say the name of the dictionary,
and then in square brackets the key,
whose key-value pair we want to delete,
that's going to get rid of that key-value pair from our dictionary.
So, here we're saying del inventory sub pears.
So, here our key-value pair pears is associated with the value 217.
Line 3 says get rid of this key-value pair.
So, when I actually run line 3,
you'll notice the number of key-value pairs will go from four to three,
if instead we created the same dictionary.
So again, we're back to having four key-value pairs.
Now, in line 3, rather than deleting the value associated with pears,
then we're going to set it to zero.
So, if you remember from when we first introduced dictionaries,
we can set a key-value pair by saying the name of the dictionary, sub,
and then whatever key we want to set,
equals whatever value we want it to be associated with.
But the difference here is that by the time we run line 3,
we'll already have a key-value pair that associates
the key pears with the different value 217.
So, when we say inventory sub pears now equals zero,
that's going to update the value associated with the key pairs.
So, in other words, line 3 is going to say pears is not 217 anymore,
it's now associated with the value zero.
So here, if we now run line 3,
then you'll see the value of pears is now zero.
So, let's reuse that dictionary in another example.
So on line 1, we create the same dictionary that
has four key-value pairs that we saw from the previous examples.
Now, on line 2,
we're setting that dictionary,
so inventory sub bananas to inventory sub bananas plus 200.
Now, when we look at this,
this expression is a little confusing because here we're
repeating this expression inventory sub bananas.
But remember that when we do in assignment,
then what Python does is it first
evaluates the value that we're going to be assigning to.
So, the first thing Python does on line 2 is it asks what's the value of this expression,
inventory sub bananas plus 200?
After it computes this value,
then it's going to assign whatever that value is to inventory sub bananas.
So, in other words, to figure out what this is going to do,
we first have to ask what's the value of this expression?
So, to figure out the value of this overall expression, let's break it down.
So, we add inventory sub bananas to 200,
so we ask what's the value of inventory sub bananas?
To figure that out, we look up this dictionary and see that
bananas is associated with 312.
So this value is 312.
Then, we add 200 to that to get 512.
Then, Python takes this integer 512 and it
makes it the new value associated with the key bananas.
So, this is going to now be 512 when we run line 2.
So, let's run line 2, and we see bananas is now 512.
Now, if you remember the "len" function from strings or lists or tuples,
remember that len gives you the number of items in a collection.
So, in the case of strings,
len gives us the number of characters in that string.
Len also works with dictionaries.
So, if we pass in the dictionary inventory,
then when we call len on it,
the value of this expression gives us the number of key-value pairs in this dictionary.
So, the value of this expression is going to be one,
two, three, four, because there are four key-value pairs.
So, we'll see you that "numItems" in
our frame right here is going to be assigned to the value, the integer four.
So, let's run line 4 and we see that numItems is now four.
So, let's do some more questions.
In this question we ask what is printed by the following statements?
So, here we create a dictionary with three key-value pairs.
Then, we say my dictionary sub mouse equals my dictionary sub cat,
plus my dictionary sub dog.
So, the value of this expression,
my dictionary sub cat, is 12.
The value of this expression,
my dictionary sub dog, is six,
meaning that the value of this overall expression,
my dictionary sub cat plus my dictionary sub dog is 12 plus 6 or 18.
So, then we assign the value 18 to being associated with the key mouse.
So, by the time we print out my dictionary sub mouse,
we're going to print out 18.
So our answer is C. So,
this question asks us to update the value for Phelps in the dictionary swimmers,
to include his medals from the Rio Olympics by adding five to the current value.
So, Phelps will now have 28 total medals,
and it asks us do not rewrite the dictionary.
So, here on line 2,
we assigned swimmers to be a dictionary that has one, two, three,
four, five, six key-value pairs,
but we want to update the value associated with the key Phelps.
In the way that we want to update it is by adding five to the current value.
So, the way that we're going to do that is we're going to say,
swimmers sub Phelps is now its previous value.
So swimmers sub Phelps equals swimmers sub Phelps plus five.
So, whatever it started out with,
it's now going to be that, plus five.
That's all for now. Until next time.